package com.ims.iaa.JavaScript;



import org.mozilla.javascript.*;

public class MyFactory extends ContextFactory
{

    // Custom Context to store execution time.
    private static class MyContext extends Context
    {
        long startTime;
    }

    static {
        // Initialize GlobalFactory with custom factory
        ContextFactory.initGlobal(new MyFactory());



    }

    // Override makeContext()
    protected Context makeContext()
    {

        MyContext cx = new MyContext();
        // Use pure interpreter mode to allow for
        // observeInstructionCount(Context, int) to work
        cx.setOptimizationLevel(-1);
        //cx.setGeneratingDebug(true);
        // Make Rhino runtime to call observeInstructionCount
        // each 10000 bytecode instructions
        cx.setInstructionObserverThreshold(10000);
        cx.setLanguageVersion(Context.VERSION_1_7);

        return cx;

    }

    // Override hasFeature(Context, int)
    public boolean hasFeature(Context cx, int featureIndex)
    {
    	
        // Turn on maximum compatibility with MSIE scripts
        switch (featureIndex) {
            case Context.FEATURE_NON_ECMA_GET_YEAR:
                return true;

            case Context.FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME:
                return true;

            case Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER:
                return true;

            case Context.FEATURE_LOCATION_INFORMATION_IN_ERROR:
                return true;
            case Context.FEATURE_ENHANCED_JAVA_ACCESS:
            	return true;
            case Context.FEATURE_PARENT_PROTO_PROPERTIES:
            	return true;
            case Context.FEATURE_STRICT_VARS:
            	return false;

            	
        }
        return super.hasFeature(cx, featureIndex);
    }

    // Override observeInstructionCount(Context, int)
    protected void observeInstructionCount(Context cx, int instructionCount)
    {
        MyContext mcx = (MyContext)cx;
        long currentTime = System.currentTimeMillis();
        if (currentTime - mcx.startTime > 3000*1000) {
            // More then 30 seconds from Context creation time:
            // it is time to stop the script.
            // Throw Error instance to ensure that script will never
            // get control back through catch or finally.
            throw new Error();
        }
    }

    // Override doTopCall(Callable, Context, Scriptable scope, Scriptable thisObj, Object[] args)
    protected Object doTopCall(Callable callable,
                               Context cx, Scriptable scope,
                               Scriptable thisObj, Object[] args)
    {
        MyContext mcx = (MyContext)cx;
        mcx.startTime = System.currentTimeMillis();

        return super.doTopCall(callable, cx, scope, thisObj, args);
    }

}
